package com.xiaoshuidi.cloud.module.iot.task;

import com.alibaba.fastjson.JSONObject;
import com.xiaoshuidi.cloud.framework.tenant.core.aop.TenantIgnore;
import com.xiaoshuidi.cloud.module.iot.api.device.vo.WaterMeterCommonResponseModel;
import com.xiaoshuidi.cloud.module.iot.common.ResultModel;
import com.xiaoshuidi.cloud.module.iot.dal.model.ammeter.AmmeterInfoReqModel;
import com.xiaoshuidi.cloud.module.iot.api.device.vo.AmmeterInfoResModel;
import com.xiaoshuidi.cloud.module.iot.dal.model.watermeter.WaterMeterCommonRequestModel;
import com.xiaoshuidi.cloud.module.iot.dal.pojo.TbAccountDo;
import com.xiaoshuidi.cloud.module.iot.dal.pojo.TbDeviceDo;
import com.xiaoshuidi.cloud.module.iot.dal.repository.DeviceRepository;
import com.xiaoshuidi.cloud.module.iot.dal.repository.TbAccountRepository;
import com.xiaoshuidi.cloud.module.iot.enums.BrandEnum;
import com.xiaoshuidi.cloud.module.iot.enums.DeviceTypeEnum;
import com.xiaoshuidi.cloud.module.iot.service.AmmeterService;
import com.xiaoshuidi.cloud.module.iot.service.WaterMeterService;
import com.xxl.job.core.handler.annotation.XxlJob;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.stream.Collectors;

import static cn.hutool.core.lang.Console.log;

/**
 * @BelongsProject: lease-module-new-iot
 * @BelongsPackage: com.xiaoshuidi.cloud.module.iot.task
 * @Author: James
 * @CreateTime: 2023-06-26  14:44
 * @Version: 1.0
 */
@Component
public class YinTongHardwareTask {
    private final static Logger LOGGER = LoggerFactory.getLogger(YinTongHardwareTask.class);
    @Autowired
    TbAccountRepository tbAccountRepository;
    @Autowired
    DeviceRepository deviceRepository;
    @Autowired
    private AmmeterService ammeterService;
    @Autowired
    private WaterMeterService waterMeterService;
    @XxlJob("iot-ammter-task")
    @TenantIgnore
    public void ammterTask(){
        List<TbAccountDo> tbAccountDos=tbAccountRepository.getApiListByBrandNo(BrandEnum.YINTONGWULIAN.getBrand());
        for (TbAccountDo tbAccountDo : tbAccountDos) {
            String apiNo = tbAccountDo.getApiNo();
            //查询该tb_account下挂的所有设备
            List<TbDeviceDo> deviceListByOneApiNo = deviceRepository.getDeviceListByOneApiNo(apiNo);
            //电表设备
            List<TbDeviceDo> ammeterList = deviceListByOneApiNo.stream().filter(tbDeviceDo -> DeviceTypeEnum.AMMETER.getCode().equals(tbDeviceDo.getDeviceType())).collect(Collectors.toList());
            //水表设备
            List<TbDeviceDo> watermeteList = deviceListByOneApiNo.stream().filter(tbDeviceDo ->
                    DeviceTypeEnum.COLD_WATERMETE_TYPE_ENUM.getCode().equals(tbDeviceDo.getDeviceType())
                            || DeviceTypeEnum.HOT_WATERMETE_TYPE_ENUM.getCode().equals(tbDeviceDo.getDeviceType())).collect(Collectors.toList());
            //刷新电表
            for (TbDeviceDo tbDeviceDo : ammeterList){
                AmmeterInfoReqModel ammeterInfoReqModel=new AmmeterInfoReqModel();
                ammeterInfoReqModel.setDeviceNo(tbDeviceDo.getDeviceNo());
                try {
                    ResultModel<AmmeterInfoResModel> ammeterInfoResModelResultModel=ammeterService.refreshAmmeterInfo(ammeterInfoReqModel);
                    LOGGER.info("Single-定时任务银通物联电表刷新结果:{}", JSONObject.toJSONString(ammeterInfoResModelResultModel));
                }catch (Exception e){
                    e.printStackTrace();
                    LOGGER.error("All-定时任务银通物联电表刷新结果,错误原因：{}", e.getMessage());
                }
            }
            //刷新水表
            for (TbDeviceDo tbDeviceDo : watermeteList){
                WaterMeterCommonRequestModel waterMeterCommonRequestModel=new WaterMeterCommonRequestModel();
                waterMeterCommonRequestModel.setDeviceNo(tbDeviceDo.getDeviceNo());
                try {
                    ResultModel<WaterMeterCommonResponseModel> waterMeterCommonResponseModelResultModel = waterMeterService.refreshWatermeterInfo(waterMeterCommonRequestModel);
                    LOGGER.info("Single-定时任务银通物联水表刷新结果:{}", JSONObject.toJSONString(waterMeterCommonResponseModelResultModel));
                }catch (Exception e){
                    e.printStackTrace();
                    LOGGER.error("All-定时任务银通物联水表刷新结果,错误原因：{}", e.getMessage());
                }
            }

        }
    }
}
